#ifndef AFTERDETECT_H
#define AFTERDETECT_H
#include "imagerecord.h"

#include <QMutex>
#include <QObject>
#include <QThread>
#include <QFile>
#include <QTextStream>
#include "quicktool.h"

class AfterDetect : public QObject {
    Q_OBJECT
public:
    const QString ConfigPath = "master/history/";
    static AfterDetect& getInstance();

    std::vector<int> getCacheItemById(int id);
    int getImageId();
    int addImageId();
    bool getCacheWaste(int index);
    void removeCacheDetect(int id, int defect);
    void inPlaceadjustCache(int currentIndex, int offset);
    bool containsDefect(int index, int defect);
    ImageRecord lastCard;
    void inPlaceadjustCacheremove34(int currentIndex, int offset);
    bool getCacheWasteAnd24(int index);
    void addCacheDetect(int id, int defect);
public slots:
    void finishAddDetectCacheSlot(int id, ImageRecord imgCard);
    void clearCache();
    void valueTableLoad();
    void getFinishProcess(ImageRecord imgCard);
    void fileNameChangedSlot();
    QMap<int, std::vector<int>> getCurrentCache() const {return Cache_;};
    QVector<std::vector<int>> getTotalCache() const {return TotalCache_;};

signals:
    void moveDistanceSign(float distance);
    void addDataItem(ImageRecord imgCard);

private:
    AfterDetect(QObject *parent = nullptr);
    ~AfterDetect();
    AfterDetect(const AfterDetect&) = delete;
    AfterDetect& operator=(const AfterDetect&) = delete;

    QThread threadAfterDetect;
    QMutex mutex_;
    QMap<int, std::vector<int>>  Cache_;
    QVector<std::vector<int>> TotalCache_;
    QFile fileStream;
    QTextStream out;
    int totailId;
    int imageCardId = 0;


    QString configFileName;
    void initDirectory();
    bool loadCacheFromFile();
    void appendToTotalCache(const std::vector<int> &item);
    void appendCacheToFile(const std::vector<int> &item);
    void clearTotalCache();
    void defectComp(int currentID);
};

#endif // AFTERDETECT_H
